package ase;

import java.io.IOException;

public class ASEMethods {

	private TCPClient client;
	private boolean productDone = false;

	public ASEMethods(TCPClient client){

		this.client = client;
	}

	public boolean toloranceCheck(double weight, double tolorance, double nomWeight) { //Tolorance chech
		if(((nomWeight-weight)/(nomWeight)) <= tolorance && 0-tolorance <=((nomWeight-weight)/(nomWeight))){ //Gets the percentage diversity (+/-)
			return true;
		}
		else{
			return false;
		}	
	}

	public boolean cancelProcedure() { //Cancel procedure
		boolean cancelConfirm = rm49("Confirm cansellation af procedure"); //Asks user to end procedure
		if(cancelConfirm){
			return setProductDone(true);
		}
		else{
			return setProductDone(false);
		}
	}

	public int rm20_8(String input){ //The RM20 8 command
		try {
			String s[] = client.command("RM20 8 \"" + input +"\" " + "\"\" " + "\"&3\"").toUpperCase().split(" ");
			if(s.length>=3){
				if(s[2].length() <= 2){
					return 0;
				}
			}
			if(!s[1].equals("A")){
				return 0;
			}
			else{
				return Integer.parseInt(s[2].substring(1, s[2].length()-1));
			}
		} catch (IOException e) {
			return 0;
		}
	}

	public boolean rm49(String input){ //RM49 4 command
		try {
			String s[] = client.command("RM49 4 \"" + input + "\"").toUpperCase().split(" ");
			return s[2].equals("1")?true:false;
		} catch (IOException e) {
			return false;
		}
	}

	public String rm3x(String input){ //The RM3X command for set of softkeys
		try{
			String s = client.command("RM3" + input).toUpperCase();
			return s;
		} catch (IOException e) {
			return null;
		}
	}

	public double tara(){ // The tara command
		try {
			String[] a = client.command("T").split(" ");
			return Double.parseDouble(a[7]);
		} catch (IOException e) {
			return 0.0;
		}
	}

	public double getNetWeight(){ //Get the nettoweigt on the weight
		try {
			String[] a = client.command("S").split(" ");
			if(Double.parseDouble(a[7])<0){
				return -1;
			}
			else{
				return Double.parseDouble(a[7]);
			}
		} catch (IOException e) {
			return 0.0;
		} catch (NumberFormatException nf){
			return 0.0;
		}
	}

	public int cancelChoice(int step, int nextStep){ //Cancel has been pressed on weight, and user is asked to confirm this cancel or not		
		boolean confirmCancel = rm49("Cancel?");
		if(confirmCancel){								//Cancel has been confirmed and cancel procedure is activated
			cancelProcedure();
			return step;
		}
		else{
			return nextStep;
		}
	}

	public int confirm(String message, int step, int nextStep){
		boolean confirm = rm49(message); //Message to confirm
		if(confirm){ 
			return nextStep;
		}
		else{ //confirm denied 
			cancelChoice(step, nextStep);
			return step;
		}		
	}
	public int confirmCancel(String message, int step, int stepReturn){
		boolean confirm = rm49(message); //Cancel has been pressed on weight, and user is asked to confirm this cancel or not
		if(confirm){ //Cancel has been confirmed and cancel procedure is activated		
			cancelProcedure();
			return step;
		}
		else{ //Cancel has been aborted
			return stepReturn;
		}		
	}

	public boolean isProductDone() {
		return productDone;
	}

	public boolean setProductDone(boolean productDone) {
		this.productDone = productDone;
		return productDone;
	}

}
